package wmh.day01092.service;


import wmh.day01092.constants.Tour_RouteType;
import wmh.day01092.dao.Scenic_SpotDao;
import wmh.day01092.dao.Tour_RouteDao;
import wmh.day01092.po.Scenic_Spot;
import wmh.day01092.po.Tour_Route;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

public class TravelMgr {
    /**
     *
     */

    public void showTravelInfo(Tour_RouteDao tour_routeDao, Scenic_SpotDao scenic_spotDao, Scanner sc) {
        // 创建一个Map集合对象,作为条件查询参数,并初始化Map集合对象
        Map<String, Object> map = new HashMap<>();
        // 查询条件 1 根据游玩日期进行条件筛选
        System.out.println("\n【查询条件1】根据游玩日期进行条件筛选");
        System.out.print("请输入游玩日期(不输入任何字符串信息,默认不查询该条件):");
        String tour_routeTime = sc.nextLine();
        // if输入字符串数据,校验输入的时间格式是否正确
        if (tour_routeTime != null && !"".equals(tour_routeTime)) {
            // 正则表达式,校验时间(日期)格式
            String timeReg = "\\d{4}-\\d{1,2}-\\d{1,2}";
            while (!tour_routeTime.matches(timeReg)) {
                System.out.print("\n出行日期时间输入格式有误,请重新输入:");
                tour_routeTime = sc.nextLine();
            }
            // 添加出行日期时间
            map.put("tour_routeTime", tour_routeTime);
        }

        // 查询条件 2 根据游玩类型进行条件筛选
        System.out.println("\n【查询条件2】根据游玩类型进行条件筛选");
        // 获取游玩类型枚举类所有数据
        Tour_RouteType[] tour_routeType = Tour_RouteType.values();
        //定义一个字符串增强类,用来拼接字符信息
        StringBuffer str = new StringBuffer();
        for (int i = 0; i < tour_routeType.length; i++) {
            str.append(tour_routeType[i].getCode() + ":" + tour_routeType[i].getValue() + "\t");
        }
        System.out.print("请选择游玩类型(不输入任何字符串信息,默认不查询该条件):" + str);
        String tourType = sc.nextLine();
        if (tourType != null && !"".equals(tourType)) {
            //添加游玩类型值
            map.put("tour_routeType", tourType);
        }
        System.out.println("\n显示所有符合的景点");
        List<Tour_Route> tour_routeList = tour_routeDao.queryTour_Route(map);
        System.out.println("景点编号\t\t景点名称\t\t\t类型\t\t价格\t\t\t咨询电话\t\t咨询人\t\t创建日期");
        for (Tour_Route t : tour_routeList) {
            System.out.println(map.get(t.getTR_ID()) + "\t\t" + t.getTR_NAME() + "\t\t" + t.getTR_TYPE() + "\t\t" + t.getTR_PRICE() + "\t\t" + t.getTR_PHONE() + "\t\t" + t.getTR_USER() + "\t\t" +
                    "\t\t" + t.getCREATE_TIME());
        }

    }

    /**
     * 录入出差行程信息
     *
     * @return
     * @paramschedule
     */
    public void saveTour_RouteInfo(Tour_RouteDao tour_routeDao, Scenic_SpotDao scenic_spotDao, Scanner sc) {
        Tour_Route tour_route = new Tour_Route();
        Scenic_Spot scenic_spot = new Scenic_Spot();
        System.out.println("添加游玩线路");
        System.out.println("请输入线路名称（*）");
        String TrName = sc.nextLine();
        tour_route.setTR_NAME(TrName);
        System.out.println("请输入游玩类型（*）");
        String TrType = sc.nextLine();
//        //游玩类型
//        Map<Integer, String> mapTravel =qu();
//        StringBuffer tripStr = new StringBuffer();
//        for (Integer key : mapTravel.keySet()) {
//            tripStr.append(key + "." + mapTravel.get(key) + "\t");
//        }
//        System.out.println("请选择游玩类型：".concat(tripStr.toString()));
        tour_route.setTR_NAME(TrType);
        System.out.println("请输入价格：");
        int TrPrice = sc.nextInt();
        tour_route.setTR_PRICE(TrPrice);
        System.out.println("请输入包含景点：");
        String SsName = sc.nextLine();
        scenic_spot.setSS_NAME(SsName);
        System.out.println("请输入咨询人：");
        String TrUser = sc.nextLine();
        tour_route.setTR_USER(TrUser);
        System.out.println("请输入咨询电话：");
        String TrPhone = sc.nextLine();
        tour_route.setTR_PHONE(TrPhone);
        System.out.println("请输入游玩日期：");

        String TrTime = sc.nextLine();
        while (!TimeCheckedUtils.checkTime(TrTime)) {
            System.out.println("可能格式输入有误，请重新输入！");
            TrTime = sc.nextLine();
        }
         tour_route.setCREATE_TIME(TrTime);
        //调用保存信息行程方法
        tour_route.setTR_NAME(TrName);
        tour_route.setTR_TYPE(Integer.parseInt(TrType));
        tour_route.setTR_PRICE(TrPrice);
        scenic_spot.setSS_NAME(SsName);
        tour_route.setTR_USER(TrUser);
        tour_route.setTR_PHONE(TrPhone);
        tour_route.setCREATE_TIME(TrTime);
        int addRes = tour_routeDao.saveTour_Route(tour_route);
        System.out.println(addRes > 0 ? "添加成功" : "添加失败！！");

    }

    public void updateTour_RouteInfo(Tour_RouteDao tour_routeDao, Scenic_SpotDao scenic_spotDao, Scanner sc) {
        Tour_Route tour_route = new Tour_Route();
        System.out.println("请输入取消游玩线路的名称：");
        String TrName = sc.nextLine();
        tour_route.setTR_NAME(TrName);
        Map<String,Object> map = new HashMap<>();
        map.put("TrName",TrName);
        // 根据线路名称获取对应ID
        List<Tour_Route> tour_routeList = tour_routeDao.queryTour_Route(map);
        if (tour_routeList != null && tour_routeList.size() > 0 ) {
            // 获取到对应行程数据
            tour_route.setTR_ID(tour_routeList.get(0).getTR_ID());
            tour_route.setTR_TYPE(1);
            int updateRes = tour_routeDao.updateTour_RouteByType(tour_route);
            System.out.println(updateRes > 0 ?  TrName + "线路行程已取消!!!" :  TrName + "线路行程取消失败!!!");
        }else{
            System.out.println(TrName + "线路信息不在平台中");
        }
    }

    /**
     * 删除行程信息
     *
     */
    public void deletetour_routeDaoInfo(Tour_RouteDao tour_routeDao, Scenic_SpotDao scenic_spotDao, Scanner sc) {
        Tour_Route tour_route = new Tour_Route();
        System.out.println("删除游玩线路计划");
        System.out.println("请输入游玩线路计划编号（*）");
        int Tr_ID = sc.nextInt();
        tour_route.setTR_ID(Tr_ID);
        int deleteRes = (tour_route.getTR_ID());
        System.out.println(deleteRes> 0 ? "信息删除成功" : "信息删除失败！");
        }

}
